
const defaultWidget = 'el-input'

export default {
  data() {
    return {
      defaultConfig: {
        column: 24,
        hidden: false
      }
    }
  },
  computed: {
    label() {
      if (this.ui.showLabel === false) {
        return ''
      }
      return this.ui.label || ''
    },
    // 一些配置
    mergeConfig() {
      return Object.assign({}, this.defaultConfig || {}, this.schema.config || {})
    },
    // 与UI显示相关的一些东西
    ui() {
      // 找出ui: 表达式的  {'ui:label': '姓名'}
      const ui = this.schema.ui || {}
      const uiEx = Object.keys(this.schema).filter(item => item.includes('ui:')).reduce((prev, next) => {
        prev[next.split(':')[1]] = this.schema[next]
        return prev
      }, {})
      const tmpUI = Object.assign({}, uiEx, ui)
      return Object.assign({}, tmpUI, {
        widget: ui.widget || uiEx.widget || defaultWidget,
        column: tmpUI.column || this.defaultConfig.column
      })
    },
    // 是否禁用
    disabled() {
      const disabled = this.ui.disabled
      if (disabled === undefined) {
        return false
      } else if (typeof disabled === 'function') {
        // 必须是 function(formData) {} this为当前的form-item
        return disabled.call(this, this.formData)
      }
      return disabled
    },
    // 是否显示
    hidden() {
      const hidden = this.ui.hidden
      if (hidden === undefined) {
        return false
      } else if (typeof hidden === 'function') {
        // 必须是 function(formData) {} this为当前的form-item
        return hidden.call(this, this.formData)
      }
      return hidden
    },
    // 规则
    rules() {
      const rules = this.schema.rules
      if (typeof rules === 'function') {
        return rules.call(this, this.formData)
      }
      return rules || []
    }
  },
  methods: {
  }
}
